﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace PacketPasser
{
    class Program
    {
        const string argFormat = " [listen port] [outgoing address:[port]]";
        static int port;
        static int destPort;
        static string hostName;

        static void Main(string[] args)
        {
            Console.WindowWidth = Console.LargestWindowWidth;
            if (args == null || args.Length < 2 || args[0] == "/?" || args[0] == "-help" || args.Length > 3)
            {
                Console.WriteLine(argFormat);
                args = Console.ReadLine().Split(' ');
                if (args == null || args.Length < 2 || args[0] == "/?" || args[0] == "-help" || args.Length > 3)
                    return;
            }
            if (!int.TryParse(args[0], out port))
            {

                Console.WriteLine(argFormat);
                Console.WriteLine("Could not read 'listen port'");
                return;
            }
            string hostName = args[1];
            if (args.Length == 2 && args[1].Contains(":") == false)
            {
                Console.WriteLine(argFormat);
                Console.WriteLine("Outgoing address must have a port(example: '127.0.0.1:80')");
            }
            destPort = 0;

            if (args.Length == 2)
            {
                var after = hostName.Substring(hostName.IndexOf(":") + 1);
                Debug.Assert(after.Contains(":") == false);
                hostName = hostName.Substring(0, hostName.IndexOf(":"));
                Debug.Assert(hostName.Contains(":") == false);

                if (!int.TryParse(after, out destPort))
                {
                    Console.WriteLine(argFormat);
                    Console.WriteLine("Outgoing port must be a number");
                    return;
                }

            }
            else if (args.Length == 3)//args length is >2
            {
                if (!int.TryParse(args[2], out destPort))
                {
                    Console.WriteLine(argFormat);
                    Console.WriteLine("Outgoing port must be a number");
                    return;
                }
            }
            var packetProxy = new PacketProxy(port, hostName, destPort);
            string input = null;
            do
            {
                if (string.IsNullOrEmpty(input) == false)
                    ProcessInput(input, ref  packetProxy);
                System.Threading.Thread.Sleep(400);
                input = Console.ReadLine();
            } while (input != string.Empty && input != "/quit");
            packetProxy.Stop();
        }
        static void Help(string input)
        {
            Console.WriteLine();
            Console.WriteLine("commands");
            var color = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("------");
            Console.ForegroundColor = color;
            Console.WriteLine("destination");
            Console.WriteLine(" - destination localhost:25565");

            Console.WriteLine("ignorefirst");
            Console.WriteLine(" - checks what ignorefirst is set to");
            Console.WriteLine("ignorefirst true");

            Console.WriteLine("ignorefirst false");

            Console.WriteLine("status");
        }

        private static void ProcessInput(string input, ref PacketProxy proxy)
        {
            if (input.StartsWith("help"))
            {
                Help(input);
                return;
            }

            if (input.StartsWith("destination "))
            {
                var newDestination = commands.ProxyCommands.ChangeDestination(input, proxy);
                if (newDestination != null)
                    proxy = newDestination;
                return;

            }

            if(input.StartsWith("proxy"))
            {
                proxy.VisitPackets = !proxy.VisitPackets;
                Console.WriteLine("proxying:" + proxy.VisitPackets);
            }

            if (input.StartsWith("ignorefirst"))
            {
                commands.ProxyCommands.IgnoreFirst(input, proxy);
                return;
            }

            if (input.StartsWith("restart"))
            {
                proxy = commands.ProxyCommands.Restart(input, proxy);
                return;
            }

            if (input.StartsWith("status"))
            {
                commands.ProxyCommands.GetStatus(input, proxy);

                return;
            }
            if(input.StartsWith("filter"))
            {
                proxy.FilterPackets = !proxy.FilterPackets;
                Console.WriteLine("Filtering:" + proxy.FilterPackets);
            }


        }


    }
}
